package org.yuxiong.utils.proxy.server;

import java.net.Proxy;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.TableRow;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;
import org.yuxiong.utils.DownloadException;



import org.yuxiong.utils.HttpUtils;
import org.yuxiong.utils.proxy.NetProxy;


public class ProxyCN {

	private static final Pattern PATTERN_IP = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d{2,4})");
	private static final NodeFilter filter = new AndFilter(new TagNameFilter("tr"), new HasAttributeFilter("bgcolor","#fbfbfb"));
	private static final String URL_FAST = "http://www.proxycn.com/html_proxy/30fastproxy-1.html";
	
	public static Stack<Server> getServers() throws DownloadException, ParserException{
		Stack<Server> rtnList = null;
		String content = HttpUtils.callURL(URL_FAST, "gb2312");

		Parser pageParser = Parser.createParser(content, "gb2312");
		NodeList nodelist = pageParser.parse(filter);
		if(null!=nodelist && nodelist.size()>0){
			rtnList = new Stack<Server>();
			for (SimpleNodeIterator i = nodelist.elements();i.hasMoreNodes();){
				TableRow node = (TableRow) i.nextNode();
				
				Matcher m = PATTERN_IP.matcher(node.getAttribute("onDblClick"));
				if(m.find()){
					rtnList.push(new Server(m.group(1), Integer.parseInt(m.group(2))));
				}
			}
		}
		return rtnList;
	}
	
	public static List<Server> getValiServers() throws ParserException, DownloadException{
		List<?> list = getServers();
		List<Server> rtnList = new ArrayList<Server>();
		for(Iterator<?> it=list.iterator();it.hasNext();){
			Server tmp = (Server) it.next();
			NetProxy np = new NetProxy();
			np.removeLocalProxy();
			Proxy proxy = np.getProxy(Proxy.Type.HTTP,tmp.getIp(),tmp.getPort());
			try {
				String str = HttpUtils.callURL("http://www.baidu.com", "gb2312", proxy);
				if(str!=null) rtnList.add(tmp);
			} catch (DownloadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rtnList;
	}
	
	public static void main(String[] args) throws DownloadException, ParserException{
		
		//System.out.println(HttpUtils.callURL("http://www.proxycn.com/html_proxy/30fastproxy-1.html", "gb2312"));
	
		String content = HttpUtils.callURL("http://www.proxycn.com/html_proxy/30fastproxy-1.html", "gb2312");

		Parser pageParser = Parser.createParser(content, "gb2312");
		//NodeFilter filter = new RegexFilter(PATTERN_IP);
		NodeList nodelist = pageParser.parse(filter);
		if(null!=nodelist && nodelist.size()>0){
			
			for (SimpleNodeIterator i = nodelist.elements();i.hasMoreNodes();){
				TableRow node = (TableRow) i.nextNode();
				
				Matcher m = PATTERN_IP.matcher(node.getAttribute("onDblClick"));
				m.find();
				System.out.println("str="+node.getAttribute("onDblClick")+", "+m.group(1));
			}
		}
	}
	
}
